<template>
  <!-- <ContentWrap>
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="IPv4 地址" prop="ipv4Address">
        <el-input v-model="queryParams.ipv4Address" placeholder="请输入IPv4 地址" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="子网掩码" prop="ipv4Prefixlen">
        <el-input v-model="queryParams.ipv4Prefixlen" placeholder="请输入子网掩码" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="子网的网络地址" prop="ipv4NetworkId">
        <el-input v-model="queryParams.ipv4NetworkId" placeholder="请输入子网的网络地址" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item label="端口ID" prop="portId">
        <el-input v-model="queryParams.portId" placeholder="请输入端口ID" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['business:ipv4-addresses:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['business:ipv4-addresses:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="danger" plain :disabled="isEmpty(checkedIds)" @click="handleDeleteBatch"
          v-hasPermi="['business:ipv4-addresses:delete']">
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap> -->

  <!-- 列表 -->
  <ContentWrap>
    <el-table border :header-cell-style="{ background: '#e6f7ef', color: '#008c46' }" row-key="id" v-loading="loading"
      :data="list" :stripe="true" :show-overflow-tooltip="true" @selection-change="handleRowCheckboxChange">
      <el-table-column label="IPv4 地址" align="center" prop="ipv4Address" />
      <el-table-column label="子网掩码" align="center" prop="ipv4Prefixlen" />
      <el-table-column label="子网地址" align="center" prop="ipv4NetworkId" />
      <el-table-column label="端口" align="center" prop="portName" />
      <!-- <el-table-column label="SNMP 上下文名称" align="center" prop="contextName" /> -->
      <!-- <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['business:ipv4-addresses:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)"
            v-hasPermi="['business:ipv4-addresses:delete']">
            删除
          </el-button>
        </template>
</el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <Ipv4AddressesForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import download from '@/utils/download'
import { Ipv4AddressesApi, Ipv4Addresses } from '@/api/business/ipv4Addresses'
import Ipv4AddressesForm from './Ipv4AddressesForm.vue'

// 声明接收的 props
const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})


/** IP地址 列表 */
defineOptions({ name: 'Ipv4Addresses' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<Ipv4Addresses[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  ipv4Address: undefined,
  ipv4Prefixlen: undefined,
  ipv4NetworkId: undefined,
  portId: undefined,
  deviceId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await Ipv4AddressesApi.getIpv4AddressesPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await Ipv4AddressesApi.deleteIpv4Addresses(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除IP地址 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await Ipv4AddressesApi.deleteIpv4AddressesList(checkedIds.value);
    checkedIds.value = [];
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: Ipv4Addresses[]) => {
  checkedIds.value = records.map((item) => item.id!);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await Ipv4AddressesApi.exportIpv4Addresses(queryParams)
    download.excel(data, 'IP地址.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
const ipv4Address = ref()
/** 初始化 **/
onMounted(() => {
  console.log("ipv4Address", props.data);
  ipv4Address.value = props.data.hostName
  queryParams.ipv4Address = ipv4Address.value
  getList()
})
</script>